package com.clp.inflxudb.core.domain;

import com.clp.inflxudb.core.enums.CompareOp;
import com.clp.inflxudb.core.enums.FluxKVType;
import lombok.Data;

import java.util.Objects;

/**
 * 属性
 */
@Data
public class FluxKV {
    /**
     * 键
     */
    private String key;

    /**
     * 值
     */
    private Object value;

    /**
     * 键的值是否为value
     */
    private CompareOp compareOp;

    private FluxKVType kvType;

    /**
     * 默认不是tag，如果是tag，需要value为String类型
     *
     * @param key：键名
     * @param value：值对象
     * @param compareOp：值比较
     */
    public FluxKV(String key, Object value, CompareOp compareOp) {
        this.key = key;
        this.value = value;
        this.compareOp = compareOp;
        this.kvType = FluxKVType.FIELD;
    }

    public FluxKV(String key, Object value, CompareOp compareOp, Boolean isTagKey) {
        this.key = key;
        if (isTagKey) {
            this.value = value.toString();
            this.kvType = FluxKVType.TAG;
        } else {
            this.value = value;
            this.kvType = FluxKVType.FIELD;
        }
        this.compareOp = compareOp;
    }

    public FluxKV(String key, Object value, CompareOp compareOp, FluxKVType kvType) {
        this.key = key;
        if (kvType == FluxKVType.TAG) {
            this.value = value.toString();
        } else {
            this.value = value;
        }
        this.compareOp = compareOp;
        this.kvType = kvType;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        FluxKV fluxKV = (FluxKV) o;
        return key.equals(fluxKV.key) && value.toString().equals(fluxKV.value.toString());
    }

    @Override
    public int hashCode() {
        return Objects.hash(key, value.toString());
    }
}
